package test19.outputstream_;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileCopy {
    public static void main(String[] args) throws IOException {
        //完成 文件拷贝，将D:\a.txt 拷贝 D:\a1.txt
        String srcFilePath = "D:\\a.txt";
        String destFilePath = "D:\\a1.txt";
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;


        try {
            fileInputStream = new FileInputStream(srcFilePath);
            fileOutputStream = new FileOutputStream(destFilePath);
            byte[] buf = new byte[1024];
            int readlen = 0;
            while((readlen= fileInputStream.read(buf)) != -1){
                //读取到后，就写入到文件 通过 fileOutputStream
                //即，是一边读，一边写
                fileOutputStream.write(buf,0,readlen);//一定要使用这个方法
            }
            System.out.println("拷贝ok~");
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if(fileInputStream != null){
                fileInputStream.close();
            }
            if (fileOutputStream != null){
                fileInputStream.close();
            }
        }
    }
}